package com.runen.infinitemovement.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.activity.MyTeamActivityV2;
import com.runen.infinitemovement.model.api.CompanyAPI;
import com.runen.infinitemovement.model.base.BaseResponseEntity;
import com.runen.infinitemovement.model.entity.TenantTitleEntity;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class NoticeFragment extends Fragment {
    @BindView(R.id.et_content)
    EditText et_content;
    @BindView(R.id.tv_update)
    TextView tv_update;
    private View view;

    public static Fragment newInstance() {
        Fragment fragment = new NoticeFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == view) {
            view = inflater.inflate(R.layout.fragment_notice, container, false);
            ButterKnife.bind(this, view);
        }
        return view;
    }

    public void initData() {
        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", ((MyTeamActivityV2) getActivity()).entity.team.companyId);
        new CompanyAPI().tenant(params, new BaseResultCallback<TenantTitleEntity>() {
            @Override
            public void onResponse(TenantTitleEntity response) {
                if (response.code == 0) {
                    et_content.setText(response.data);
                    et_content.setEnabled(false);
                    tv_update.setVisibility(((MyTeamActivityV2) getActivity()).isLeader ? View.VISIBLE : View.GONE);
                } else {
                    ToastUtils.showShort(response.msg);
                }
            }
        });
    }

    @OnClick(R.id.tv_update)
    public void onViewClick() {
        et_content.setEnabled(!et_content.isEnabled());
        if (tv_update.getText().toString().equals("修改")) {
            tv_update.setText("保存");
            et_content.setEnabled(true);
            et_content.setFocusable(true);
            et_content.setSelection(et_content.getText().toString().length());
            CommonUtil.showKeyboard(et_content);
        } else {
            tv_update.setText("修改");
            et_content.setEnabled(false);
            update();
        }
    }

    public void update() {
        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", ((MyTeamActivityV2) getActivity()).entity.team.companyId);
        params.put("announcement", et_content.getText().toString());
        new CompanyAPI().updateTenant(params, new BaseResultCallback<BaseResponseEntity>() {
            @Override
            public void onResponse(BaseResponseEntity response) {
                if (response.getCode() == 0) {
                    ToastUtils.showShort("修改成功");
                } else {
                    ToastUtils.showShort(response.getMessage());
                }
            }
        });
    }
}
